﻿namespace WinBudget
{
    using AutoItX3Lib;
    using GalaSoft.MvvmLight.Command;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Windows.Input;
    using System.Xml;

    public class AccountListViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<Accounts> accountList = new ObservableCollection<Accounts>();
        private string accountName;
        ICommand AddAccountCommand { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public AccountListViewModel()
        {
            ReadAllAccountsFromXml();
            AddAccountCommand = new RelayCommand<object>(AddAccountToListAndSave);
        }

        private void ReadAllAccountsFromXml()
        {           
            XmlTextReader reader = new XmlTextReader("Accounts.xml");

            //  Loop over the XML file
            while (reader.Read())
            {
                //  Here we check the type of the node, in this case we are looking for element
                if (reader.NodeType == XmlNodeType.Element)
                {
                    //  If the element is "accounts"
                    if (reader.Name == "Accounts")
                    {
                        var account = new Accounts();
                        account.AccountName=reader.Value;
                        AccountList.Add(account);
                    }
                }
            }

            reader.Close();
        }

        private void AddAccountToListAndSave(object obj)
        {
            var account = new Accounts();
            account.AccountName = AccountName;
            AccountList.Add(account);
            //SaveListToXml();
        }

        private void SaveListToXml()
        {
            using (XmlWriter writer = XmlWriter.Create("Accounts.xml"))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Accounts");

                foreach (Accounts accounts in accountList)
                {
                    writer.WriteStartElement("Accounts");

                    writer.WriteElementString("Account_Name", accounts.AccountName.ToString());                  

                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

        }
        
        public ObservableCollection<Accounts> AccountList
        {
            get { return accountList; }
            set
            {
                accountList = value;
                OnPropertyChanged("AccountList");
            }
        }
        
        public string AccountName
        {
            get { return accountName; }
            set
            {
                accountName = value;
                OnPropertyChanged("Account_Name");
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
